package ${package.Controller};

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlyy.his.common.api.CommonResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  ${table.entityPath}前端控制器  RestController注解 将结果以JSON形式返回
 * </p>
 *
 * @author
 * @since ${date}
 */
@RestController
@RequestMapping("/${table.entityPath}d")
public class ${entity}Controller {
    @Resource
    public ${table.serviceName} ${table.entityPath}Service;

    /**
     * 保存修改公用 POST请求方式
     * @param ${table.entityPath} 保存的对象
     * @return JsonReturn
     */
    @PostMapping("/save")
    public CommonResult save(${entity} ${table.entityPath}) {
        boolean save = ${table.entityPath}Service.save(${table.entityPath});
        if (save){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }

    /**
    * 保存修改公用 POST请求方式
    * @param ${table.entityPath} 根据id修改对象
    * @return JsonReturn
    */
    @PostMapping("/update")
    public CommonResult update(${entity} ${table.entityPath}) {
        boolean update = ${table.entityPath}Service.updateById(${table.entityPath});
        if (update){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }

    /**批量删除 支持POST
     * @param ids Long 类型 List 集合
     * @return JsonReturn
     */
    @PostMapping("remove")
    public CommonResult delete(@RequestBody List<String> ids) {
        boolean rem = ${table.entityPath}Service.removeByIds(ids);
        if (rem){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询一个  支持 GET
     *
     * @param id 查找对象的主键ID
     * @return JsonReturn
     */
    @GetMapping("findOne/{id}")
    public CommonResult findOne(@PathVariable String id) {
        ${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id);
        return CommonResult.success(${table.entityPath});
    }


    /**查询所有 支持GET ,未传当前页以及分页长度 则默认1页 10条数据
     * @param pageNum 当前页
     * @param pageSize 每页最大数据数
     * @return JsonReturn
     */
    @GetMapping("findAll")
    public CommonResult findAll(Integer pageNum, Integer pageSize) {
        Page<${entity}> page = ${table.entityPath}Service.page(new Page<${entity}>(pageNum, pageSize));
        return CommonResult.success(page);
    }
}

